<template>
  <div class="login-container">
    <div class="header">
      <div class="title">家政服务预约云平台</div>
    </div>
    <!-- 中间 -->
    <div class="center">
      <!-- 表单 -->
      <div class="form">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >
          <div
            style="
              text-align: center;
              height: 60px;
              line-height: 60px;
              font-weight: bold;
            "
          >
            用户名密码登录
          </div>
          <el-form-item prop="username">
            <el-input

              ref="username"
              v-model="loginForm.username"
              placeholder="Username"
              type="text"
            />
          </el-form-item>
          <el-tooltip
            v-model="capsTooltip"
            content="Caps lock is On"
            placement="right"
            manual
          >
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                class="inputPsd"
                :type="passwordType"
                placeholder="Password"
              />
            </el-form-item>
          </el-tooltip>
          <div>
            <el-button
              class="loginBtn"
              :loading="loading"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >登录</el-button>
          </div>
        </el-form>
        <!-- 表单 -->
      </div>
    </div>
  </div>
</template>

<script>
import { post } from '@/utils/request'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      callback()
    }
    return {
      visible: false,
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 提交注册
    submitHandler() {
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          const url = '/user/register'
          post(url, this.form).then((response) => {
            this.$message({ type: 'success', message: response.message })
            this.visible = false
          })
        } else {
          return false
        }
      })
    },

    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate()
    },
    // 去保存
    toRegisterHandler() {
      this.form = {}
      this.visible = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
/* reset element-ui css */
.login-container {
  position: absolute;
  top: 0;
  bottom: 0;
  overflow: hidden;
  margin: 0;
  width: 100%;
  height: 100vh;
  font-family: "Exo", sans-serif;
  color: #fff;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;

  .bg {
    height: 400px;
    position: absolute;
    width: 3000px;
    top: -100px;
    right: -200px;
    transform: rotate(10deg);
  }
  .header {
    height: 300px;
    position: relative;
    color: #ffffff;
    .title {
      text-align: center;
      position: absolute;
      bottom: 150px;
      width: 100%;
      font-size: 2em;
      color: #fff;
    }
  }
  .center {
    box-sizing: border-box;
    position: relative;
    top: -100px;
    width: 500px;
    margin: 0 auto;
    border-radius: 5px;
    padding: 2em;
    box-shadow: 0 0 10px #ccc;
  }
  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    line-height: 3em;
    text-align: center;
    font-size: 12px;
    font-weight: bold;
  }
}
.loginBtn {
  color: #fff;
  font-family: "Exo", sans-serif;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
}
* {
  box-sizing: border-box;
}
:root {
  --hue: 220;
}
body {
  background: hsl(var(--hue), 100%, 5%);
  height: 100vh;
  overflow: hidden;
}
.star {
  --hue: 55;
  border-radius: 50%;
  background: hsl(var(--hue), 100%, 50%);
  box-shadow: 0 0 5px 1px hsl(var(--hue), 100%, 85%);
  height: calc(var(--size, 2) * 1px);
  position: absolute;
  width: calc(var(--size, 2) * 1px);
  left: calc(var(--x, 0) * 1vw);
  opacity: 0.5;
  top: calc(var(--y, 0) * 1vw);
  -webkit-animation: flicker calc(var(--duration, 2) * 1s)
    calc(var(--delay, 10) * 1s) infinite steps(1);
  animation: flicker calc(var(--duration, 2) * 1s) calc(var(--delay, 10) * 1s)
    infinite steps(1);
}
.hill {
  background: hsl(var(--hue), 100%, 2%);
  position: fixed;
  bottom: 0;
  left: 0;
  height: 25vmin;
  width: 68vmin;
  border-radius: 0 100% 0 0;
}
.hill:after {
  content: "";
  position: absolute;
  height: 15vmin;
  width: 50vmax;
  background: hsl(var(--hue), 100%, 2%);
  left: 0;
  bottom: 0;
  border-radius: 0 100% 0 0;
}

.lights {
  width: 200vmax;
  height: 100vmax;
  position: absolute;
  left: 50%;
  top: 0;
  -webkit-transform: translate(-20%, -55%) rotate(30deg);
  transform: translate(-20%, -55%) rotate(30deg);
  display: -webkit-box;
  display: flex;
  -webkit-filter: blur(75px);
  filter: blur(75px);
}
.light {
  -webkit-box-flex: 1;
  flex: 1;
  --color-one: hsla(var(--hue-1), 100%, 50%, var(--alpha-1));
  --color-two: hsla(var(--hue-2), 100%, 50%, var(--alpha-2));
  --color-three: hsla(var(--hue-3), 100%, 50%, var(--alpha-3));
  --up: calc(var(--y, 10) * 1vmax);
  --left: calc(var(--x, 2) * 1vmax);
  background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    from(transparent),
    color-stop(10%, var(--color-one)),
    color-stop(transparent),
    color-stop(40%, var(--color-two)),
    color-stop(transparent),
    color-stop(60%, var(--color-three))
  );
  background-image: linear-gradient(
    0deg,
    transparent,
    var(--color-one) 10%,
    transparent,
    var(--color-two) 40%,
    transparent,
    var(--color-three) 60%
  );
  background-size: 100% 40vmax;
  background-position: center bottom;
  background-repeat: no-repeat;
  -webkit-transform: translate(var(--left), var(--up)) scale(1);
  transform: translate(var(--left), var(--up)) scale(1);
  -webkit-animation: shift calc(var(--duration, 2) * 1s)
    calc(var(--delay, 0) * -1s) infinite ease;
  animation: shift calc(var(--duration, 2) * 1s) calc(var(--delay, 0) * -1s)
    infinite ease;
}
@media (max-width: 768px) {
  .light {
    --up: calc(var(--y, 10) * 2vmax);
    --left: calc(var(--x, 2) * 2vmax);
  }
}
@-webkit-keyframes shift {
  50% {
    -webkit-transform: translate(0, 0) scale(1);
    transform: translate(0, 0) scale(1);
  }
}
@keyframes shift {
  50% {
    -webkit-transform: translate(0, 0) scale(1);
    transform: translate(0, 0) scale(1);
  }
}
@-webkit-keyframes flicker {
  0%,
  50%,
  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  25% {
    -webkit-transform: scale(0);
    transform: scale(0);
  }
}
@keyframes flicker {
  0%,
  50%,
  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
  }
  25% {
    -webkit-transform: scale(0);
    transform: scale(0);
  }
}
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
</style>
